package com.eightbears.beboy.ui.merchant.activity;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.RecyclerView;
import android.util.TypedValue;
import android.view.View;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.eightbears.beboy.R;
import com.eightbears.beboy.adapter.ShopTechnicianAdapter;
import com.eightbears.beboy.base.BaseActivity;
import com.eightbears.beboy.base.util.ErrorUtils;
import com.eightbears.beboy.base.util.GsonUtil;
import com.eightbears.beboy.base.util.SPUtil;
import com.eightbears.beboy.base.util.okhttp.OkHttpUtils;
import com.eightbears.beboy.base.util.okhttp.callback.StringCallback;
import com.eightbears.beboy.base.view.SwipeBackLayout;
import com.eightbears.beboy.base.view.layout.MyLinearLayoutManager;
import com.eightbears.beboy.entity.ShopTechnicianEntity;
import com.eightbears.beboy.entity.status.Status_Error;
import com.socks.library.KLog;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.Call;

import static com.eightbears.beboy.base.util.CommonAPI.CONNECTION_STATUS_SUCCESS;
import static com.eightbears.beboy.base.util.CommonAPI.KEY_HTTP_HEADER;
import static com.eightbears.beboy.base.util.CommonAPI.PARAMS_PAGE_SIZE;
import static com.eightbears.beboy.base.util.CommonAPI.URL_SHOPTECHNICIAN_LIST;
import static com.eightbears.beboy.base.util.CommonAPI.delayMillis;

/**
 * 技师页面
 */
public class ShopTechnicianActivity extends BaseActivity implements BaseQuickAdapter.RequestLoadMoreListener, SwipeRefreshLayout.OnRefreshListener {

    public static final String PARAMS_SHOPID = "shopdetail";

    @BindView(R.id.tv_save)
    TextView tv_save;
    @BindView(R.id.et_search_shop)
    EditText et_query_shop;
    @BindView(R.id.rv_list)
    RecyclerView rv_list;
    @BindView(R.id.view_empty)
    LinearLayout view_empty;
    @BindView(R.id.swipeLayout)
    SwipeRefreshLayout swipeLayout;
    @BindView(R.id.sw)
    SwipeBackLayout sw;
    private int page = 1;
    private boolean isErr = true;
    private int isEnd = 0;
    private ShopTechnicianAdapter mShopTechnicianAdapter;
    private String mShopId;

    private List<ShopTechnicianEntity.DataBean.ListBean> mList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_shop_technician);
        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
        initView();
        initData();
        initEvent();
    }

    @Override
    protected void initView() {
        ButterKnife.bind(this);
        tv_save.setVisibility(View.GONE);
        et_query_shop.setHint("请输入技师号码");
        mShopId = (String) getIntent().getExtras().get(PARAMS_SHOPID);
        initAdapter();
    }

    private void initAdapter() {
        swipeLayout.setOnRefreshListener((SwipeRefreshLayout.OnRefreshListener) mContext);
        swipeLayout.setProgressViewOffset(true, 0, (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 100, getResources().getDisplayMetrics()));
        swipeLayout.setColorSchemeColors(getResources().getColor(R.color.text_select_color),
                getResources().getColor(R.color.text_select_color),
                getResources().getColor(R.color.text_select_color),
                getResources().getColor(R.color.text_select_color));
        rv_list.setLayoutManager(new MyLinearLayoutManager(mContext));
        mShopTechnicianAdapter = new ShopTechnicianAdapter(mContext);
        mShopTechnicianAdapter.openLoadAnimation();
        mShopTechnicianAdapter.setOnLoadMoreListener(this);
        rv_list.setAdapter(mShopTechnicianAdapter);

    }

    @Override
    protected void initData() {
        getShopList();
    }

    @Override
    protected void initEvent() {
        mShopTechnicianAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                ShopTechnicianEntity.DataBean.ListBean listBean = (ShopTechnicianEntity.DataBean.ListBean) adapter.getData().get(position);
                switch (view.getId()) {
                    case R.id.iv_hips:

                        break;
                }
            }
        });

        sw.setCallBack(new SwipeBackLayout.CallBack() {
            @Override
            public void onFinish() {
                finish();
            }
        });
    }

    @OnClick(R.id.ll_back)
    void back() {
        finish();
    }


    @Override
    public void onRefresh() {
        mShopTechnicianAdapter.setEnableLoadMore(false);
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                isErr = true;
                page = 1;
                mShopTechnicianAdapter.getData().clear();
                getShopList();
                mShopTechnicianAdapter.setEnableLoadMore(true);
            }
        }, delayMillis);
    }

    @Override
    public void onLoadMoreRequested() {
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                if (isEnd == 1) {
                    mShopTechnicianAdapter.loadMoreEnd(true);
                } else {
                    if (isErr) {
                        page++;
                        getShopList();
                    } else {
                        isErr = true;
                        Toast.makeText(mContext, R.string.network_anomalies, Toast.LENGTH_LONG).show();
                        mShopTechnicianAdapter.loadMoreFail();
                    }
                }
            }
        }, delayMillis);
    }


    /**
     * 获取技师列表数据
     */
    //TODO
    public void getShopList() {
        OkHttpUtils.get().url(URL_SHOPTECHNICIAN_LIST + mShopId + "/girls")
                .addHeader(KEY_HTTP_HEADER, "Bearer " + SPUtil.getAccessToken())
                .addParams("page", page + "")
                .addParams("size", PARAMS_PAGE_SIZE)
                .build().execute(new StringCallback() {

            @Override
            public void onResponse(String response) {
                KLog.e(response);
                swipeLayout.setRefreshing(false);
                ShopTechnicianEntity shopTechnicianEntity = GsonUtil.fromJson(response, ShopTechnicianEntity.class);
                mList = shopTechnicianEntity.getData().getList();
                if (CONNECTION_STATUS_SUCCESS.equals(String.valueOf(shopTechnicianEntity.getStatus_code()))) {
                    isEnd = shopTechnicianEntity.getData().getIs_end();
                    if (page == 1) {
                        mShopTechnicianAdapter.setNewData(mList);
                        rv_list.setAdapter(mShopTechnicianAdapter);
                        if (shopTechnicianEntity.getData().getList().size() == 0)
                            view_empty.setVisibility(View.VISIBLE);
                        else
                            view_empty.setVisibility(View.GONE);
                    } else {
                        mShopTechnicianAdapter.addData(mList);
                        mShopTechnicianAdapter.loadMoreComplete();
                    }
                }
            }

            @Override
            public void onError(Call call, Exception e) {
                KLog.e(e.getMessage());
                Status_Error status_error = GsonUtil.fromJson(e.getMessage(), Status_Error.class);
                ErrorUtils.alertError((Activity) mContext, status_error);
            }

        });
    }
}
